<template>
  <HuilanBasic>
    <HuilanBasicToolbar style="margin-top: 0">
      <el-button type="primary" size="small" @click.stop="exportClick"
        >导出数据</el-button
      >
    </HuilanBasicToolbar>
    <HuilanBasicSearchbar
      :model="searchForm"
      :fields="searchFieldList"
      @reset="searchReset"
      @search="searchChange"
    >
    </HuilanBasicSearchbar>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="listData"
      ref="listRef"
      v-model="form"
      :page.sync="page"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" size="small" @click="viewExam(row)"
          >查看批阅</el-button
        >
      </template>
    </avue-crud>
  </HuilanBasic>
</template>
<script>
import { baseURL } from "@/api/common";
import { getAnswerSheet } from "@/api/exam/markExamPapers";
import { deepClone } from "@/util/util";
import serviceList from "@/config/serviceList";
import { getToken } from "@/util/auth";
export default {
  name: "AicosFrontAnswerSheet",

  inject: ["desktopWindow", "desktop"],
  props: {
    propParams: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      // search table
      searchFieldList: [
        {
          prop: "keyWords",
          clearable: true,
          placeholder: "请输入用户名",
        },
        {
          prop: "realName",
          clearable: true,
          placeholder: "请输入姓名",
        },
        {
          prop: "reviewStatus",
          placeholder: "阅卷状态",
          type: "select",
          clearable: true,
          dicData: [
            {
              label: "已阅卷",
              value: 1,
            },
            {
              label: "未阅卷",
              value: 0,
            },
          ],
        },
        {
          prop: "department",
          clearable: true,
          placeholder: "请输入部门",
        },
      ],
      searchForm: {
        reviewStatus: "",
        keyWords: "",
      },
      //table
      selectRow: {},
      form: {},
      loading: true,
      listData: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100],
      },
      option: {
        header: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        menuWidth: 120,
        align: "center",
        menuAlign: "center",
        column: [
          {
            label: "用户名",
            prop: "account",
          },
          {
            label: "姓名",
            prop: "realName",
          },
          {
            label: "部门",
            prop: "department",
          },
          {
            label: "考试时长",
            prop: "doTime",
          },
          {
            label: "客观分",
            prop: "myTotalScore",
          },
          {
            label: "主观分",
            prop: "reviewTotalScore",
          },
          {
            label: "客观题评分",
            prop: "reviewTotalScoreObjective",
          },
          {
            label: "得分",
            prop: "readScore",
          },
          {
            label: "状态",
            prop: "reviewStatus",
            type: "select",
            dicData: [
              {
                label: "已阅卷",
                value: 1,
              },
              {
                label: "未阅卷",
                value: 0,
              },
            ],
          },
          {
            label: "交卷时间",
            prop: "createTime",
            overHidden: true,
          },
        ],
      },
      examId: "",
    };
  },

  mounted() {
    if (this.propParams.testId) {
      this.examId = this.propParams.testId;
    } else if (this.desktop) {
      this.examId = this.desktopWindow.data.query.testId;
    } else {
      this.examId = this.$route.query.testId;
    }
  },

  methods: {
    viewExam(row) {
      window.open(`${baseURL}#/exam/correctExam?id=` + row.id);
    },
    exportClick() {
      window.open(
        `${baseURL}api/${
          serviceList.exam
        }/reviewExam/exportAnswerTest?token=${getToken()}&testId=${this.examId}`
      );
    },
    searchReset() {
      this.searchForm = {};
      this.onLoad(this.page);
    },
    searchChange() {
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page) {
      if (this.propParams.testId) {
        this.examId = this.propParams.testId;
      } else if (this.desktop) {
        this.examId = this.desktopWindow.data.query.testId;
      } else {
        this.examId = this.$route.query.testId;
      }
      this.loading = false;
      let obj = {
        ...this.searchForm,
        testId: this.examId,
        current: page.currentPage,
        size: page.pageSize,
      };
      getAnswerSheet(obj).then((res) => {
        const dataRes = res.data.data;
        this.page.total = dataRes.total;
        this.page.pageSize = dataRes.size;
        this.page.currentPage = dataRes.current;
        this.listData = dataRes.records;
        this.loading = false;
        this.listData.map((item) => {
          item.doTime = this.mistiming(deepClone(item.doTime));
          item.readScore =
          item.reviewStatus === 0
            ? (Number(item.myTotalScore) * 10 +
                  Number(item.reviewTotalScore) * 10) /
                10
            : (Number(item.reviewTotalScoreObjective) * 10 +
                  Number(item.reviewTotalScore) * 10) /
                10;
        });
      });
    },
    mistiming(timeStamp) {
      const newTime = this.formateTimeStamp(timeStamp);
      const str = newTime.hour + ":" + newTime.min + ":" + newTime.seconds;
      return str;
    },

    formateTimeStamp(timeStamp) {
      let hour, min, seconds;
      hour = parseInt(timeStamp / (60 * 60)); // 计算整数小时数
      let afterHour = timeStamp - hour * 60 * 60; // 取得算出小时数后剩余的秒数
      min = parseInt(afterHour / 60); // 计算整数分
      seconds = parseInt(timeStamp - hour * 60 * 60 - min * 60); // 取得算出分后剩余的秒数

      if (hour < 10) {
        hour = "0" + hour;
      }

      if (min < 10) {
        min = "0" + min;
      }

      if (seconds < 10) {
        seconds = "0" + seconds;
      }

      const restStamp = {
        hour: hour,
        min: min,
        seconds: seconds,
      };
      return restStamp;
    },
  },
  watch: {
    "desktopWindow.data": {
      handler() {
        this.onLoad(this.page);
      },
      deep: true,
    },
  },
};
</script>
<style lang="scss" scoped></style>
